import { Controller, Get, Post, Body, Request, Param, Headers, HttpCode, UseGuards } from '@nestjs/common';
import { MenuService } from './menu.service';
import { CreateMenuDto, UpdateMenuDto } from './dto/menu.dto';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { MenuResult } from './entities/menuButton.entity';
import { JwtService } from '@nestjs/jwt';
import { TokenDto } from 'src/common/auth/constants';
import { throwHttp } from 'src/utlis';
import { isTrue } from 'src/utlis/tools';
import { IdDto } from 'src/common/dto/dto';
import { CreateMenuButtonDto, UpdateMenuButtonDto } from './dto/menuButtom.dto';
import { MethodType, OperateType, OperationRecordType } from '../operation-record/entities/operation-record.entity';
import { OperationRecordService } from '../operation-record/operation-record.service'; import { MenuFieldName } from './entities/menu.entity';
@ApiBearerAuth() // swagger文档设置token
@ApiTags('菜单')
@Controller('admin/menu')
export class MenuController {
  constructor(
    private readonly menuService: MenuService,
    private readonly jwtService: JwtService,
    private readonly operationRecordService: OperationRecordService,
  ) { }
  /** 菜单列表 */
  @HttpCode(200)
  @Get('/getMenuTree')
  @ApiResponse({
    status: 200,
    description: '菜单列表',
    type: MenuResult,
  })
  @ApiOperation({
    tags: ['获取菜单列表'],
    summary: '获取菜单列表',
    operationId: 'getMenuTree',
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  getMenuTree() {
    return this.menuService.getMenuTree();
  }

  @HttpCode(200)
  @Post('/createMenu')
  @ApiBody({
    description: '创建菜单',
    type: CreateMenuDto,
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  @ApiOperation({ summary: '创建菜单', operationId: 'createMenu' })
  async createMenu(@Body() body: CreateMenuDto, @Request() req, @Headers('authorization') authorization) {
    if (!isTrue(authorization)) {
      throwHttp('获取token失败');
    }
    const token = authorization.split(' ');
    const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
    const data = await this.menuService.createMenu(body, user.userId);
    await this.operationRecordService.createOperationRecord(
      {
        operation: '创建成功',
        name: '创建菜单',
        method: MethodType.POST,
        outside: OperationRecordType.menu,
        operateType: OperateType.add,
        interfacePath: 'admin/menu/createMenu',
        ip: req.ip,
        dataId: data.id
      }, user.userId)
    return data
  }

  @HttpCode(200)
  @Post('/updateMenu')
  @ApiBody({
    description: '更新菜单',
    type: UpdateMenuDto,
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  @ApiOperation({ summary: '更新菜单', operationId: 'updateMenu' })
  async updateMenu(@Body() body: UpdateMenuDto, @Request() req, @Headers('authorization') authorization) {
    if (!isTrue(authorization)) {
      throwHttp('获取token失败');
    }
    const token = authorization.split(' ');
    const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
    const data = await this.menuService.updateMenu(body);
    if (body.diff) {
      const diff = this.operationRecordService.convertToChinese(body.diff as any[], MenuFieldName)
      await this.operationRecordService.createOperationRecord(
        {
          operation: diff,
          name: '更新菜单',
          method: MethodType.POST,
          outside: OperationRecordType.menu,
          operateType: OperateType.edit,
          interfacePath: 'admin/menu/updateMenu',
          ip: req.ip,
          dataId: data.data.id
        }, user.userId)
    }
    return data
  }

  @HttpCode(200)
  @Post('/deleteMenu')
  @ApiBody({
    description: '删除菜单',
    type: IdDto,
  })
  @ApiOperation({
    summary: '删除菜单',
    operationId: 'deleteMenu',
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  async deleteMenu(@Body() body: IdDto, @Request() req, @Headers('authorization') authorization) {
    if (!isTrue(authorization)) {
      throwHttp('获取token失败');
    }
    const token = authorization.split(' ');
    const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
    await this.operationRecordService.createOperationRecord(
      {
        operation: `删除ID: ${body.id}`,
        name: '删除菜单',
        method: MethodType.POST,
        outside: OperationRecordType.menu,
        operateType: OperateType.delete,
        interfacePath: 'admin/menu/deleteMenu',
        ip: req.ip,
        dataId: body.id
      }, user.userId)
    return this.menuService.deleteMenu(body);
  }

  @HttpCode(200)
  @Post('/createMenuButton')
  @ApiBody({
    description: '创建菜单按钮',
    type: CreateMenuButtonDto,
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  @ApiOperation({ summary: '创建菜单按钮', operationId: 'createMenuButton' })
  async createMenuButton(@Body() body: CreateMenuButtonDto, @Request() req, @Headers('authorization') authorization) {
    if (!isTrue(authorization)) {
      throwHttp('获取token失败');
    }
    const token = authorization.split(' ');
    const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
    const data = await this.menuService.createMenuButton(body);
    await this.operationRecordService.createOperationRecord(
      {
        operation: '创建成功',
        name: '创建菜单按钮',
        method: MethodType.POST,
        outside: OperationRecordType.menuButton,
        operateType: OperateType.add,
        interfacePath: 'admin/menu/createMenuButton',
        ip: req.ip,
        dataId: data.id
      }, user.userId)
    return data
  }

  @HttpCode(200)
  @Post('/updateMenuButton')
  @ApiBody({
    description: '更新菜单按钮',
    type: UpdateMenuButtonDto,
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  @ApiOperation({ summary: '更新菜单按钮', operationId: 'updateMenuButton' })
  async updateMenuButton(@Body() body: UpdateMenuButtonDto, @Request() req, @Headers('authorization') authorization) {
    if (!isTrue(authorization)) {
      throwHttp('获取token失败');
    }
    const token = authorization.split(' ');
    const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
    const data = await this.menuService.updateMenuButton(body);
    if (body.diff) {
      const diff = this.operationRecordService.convertToChinese(body.diff as any[], MenuFieldName)
      await this.operationRecordService.createOperationRecord(
        {
          operation: diff,
          name: '更新菜单按钮',
          method: MethodType.POST,
          outside: OperationRecordType.menuButton,
          operateType: OperateType.edit,
          interfacePath: 'admin/menu/updateMenuButton',
          ip: req.ip,
          dataId: data.data.id
        }, user.userId)
    }
    return data
  }

  @HttpCode(200)
  @Post('/deleteMenuButton')
  @ApiBody({
    description: '删除菜单按钮',
    type: IdDto,
  })
  @ApiOperation({
    summary: '删除菜单按钮',
    operationId: 'deleteMenuButton',
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  async deleteMenuButton(@Body() body: IdDto, @Request() req, @Headers('authorization') authorization) {
    if (!isTrue(authorization)) {
      throwHttp('获取token失败');
    }
    const token = authorization.split(' ');
    const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
    await this.operationRecordService.createOperationRecord(
      {
        operation: `删除ID: ${body.id}`,
        name: '删除菜单按钮',
        method: MethodType.POST,
        outside: OperationRecordType.menuButton,
        operateType: OperateType.delete,
        interfacePath: 'admin/menu/deleteMenuButton',
        ip: req.ip,
        dataId: body.id
      }, user.userId)
    return this.menuService.deleteMenuButton(body);
  }
}
